% MotilityDataCompilation.m
% Eden Ford
% 10 December 2023
% Code to input and process TrackMate data
% Update code in "%% User Inputs" section for appropriate data input

% General definitions:
% Object - cell or cluster of cells that are touching/interacting with one
% another

close all
clear all
clc

%% User Inputs
filename = '0mMCMP_Spots_MATLAB.xlsx'; % file name to read
sheet = 6; % sheet to read
BaseTrack = 3; % track number determined to be stationary

%% Data Import
[num_0,txt_0,raw_0] = xlsread(filename,sheet);
LocationTrack = [num_0(:,2) num_0(:,3) num_0(:,4) num_0(:,5) num_0(:,6)];

%% Organize data and subtract background movement
% sort based on track number then by frame number
LocationTrack = sortrows(LocationTrack, [1 5]);

% identify and isolate the background movement
NoiseTrack = [];
for i = 1:length(LocationTrack)
    if LocationTrack(i,1) == BaseTrack
        NoiseTrack = [NoiseTrack; LocationTrack(i,:)];
    end
end

% subtract background movement
LocationTrackBG = LocationTrack;
for i = 1:length(LocationTrack)
    for j = 1:length(NoiseTrack)
        if LocationTrack(i,5) == NoiseTrack(j,5)
            LocationTrackBG(i,2) = LocationTrack(i,2) - NoiseTrack(j,2);
            LocationTrackBG(i,3) = LocationTrack(i,3) - NoiseTrack(j,3);
        end
    end
end

%% Measurements

% (1) Track Displacement
%LocationTrackBG = LocationTrack; %%% JUST TO CHECK COLDE!!!
TrackID = 0;
Displacement1 = LocationTrackBG(1,:);
for i = 1:length(LocationTrackBG)
    if LocationTrackBG(i,1) == TrackID
        if i == length(LocationTrackBG)
            Displacement1 = [Displacement1; LocationTrackBG(i,:)];
        else
            continue
        end
    elseif LocationTrackBG(i,1) ~= TrackID
        Displacement1 = [Displacement1; LocationTrackBG(i-1,:); LocationTrackBG(i,:)];
        TrackID = TrackID + 1;
    end
end
Displacement = [];
for i = 1:length(Displacement1)
    if rem(i,2) == 0 % even
        DistanceX = Displacement1(i,2) - Displacement1(i-1,2);
        DistanceY = Displacement1(i,3) - Displacement1(i-1,3);
        Distance = sqrt(DistanceX^2 + DistanceY^2);
        Time = Displacement1(i,4) - Displacement1(i-1,4);
        Displacement = [Displacement; Displacement1(i,1), Distance, Time, Distance/Time];
    else
        continue
    end
end
Displacement;

% (2) Total Distance Traveled and Average Track Speed
%LocationTrackBG = LocationTrack; %%% JUST TO CHECK CODE!!!
TrackID = 0;
Distance = [LocationTrackBG(1,1), 0, LocationTrackBG(1,4),0];
for i = 2:length(LocationTrackBG)
    if LocationTrackBG(i,1) == TrackID
        DistanceX = LocationTrackBG(i,2) - LocationTrackBG(i-1,2);
        DistanceY = LocationTrackBG(i,3) - LocationTrackBG(i-1,3);
        DistanceH = sqrt(DistanceX^2 + DistanceY^2);
        Time = LocationTrackBG(i,4) - LocationTrackBG(i-1,4);
        Distance = [Distance; LocationTrackBG(i,1), DistanceH, Time, DistanceH/Time];
    elseif LocationTrackBG(i,1) ~= TrackID
        Distance = [Distance; LocationTrackBG(i,1), 0, LocationTrackBG(i,4),0];
        TrackID = TrackID +1;
    end
end
TrackID = 0;
TotalDistanceRow = 1;
TotalDistance = [0, 0, 0, 0]      ;
for i = 1:length(Distance)
    if Distance(i,1) == TrackID
        TotalDistance(TotalDistanceRow,2) = TotalDistance(TotalDistanceRow,2) + Distance(i,2); % sum distance traveled
        TotalDistance(TotalDistanceRow,3) = TotalDistance(TotalDistanceRow,3) + Distance(i,3); % sum time traveled
    elseif LocationTrackBG(i,1) ~= TrackID
        TotalDistanceRow = TotalDistanceRow + 1;
        TrackID = TrackID + 1;
        TotalDistance = [TotalDistance; TrackID, 0, 0, 0];
    end
end
TotalDistance(:,4) = TotalDistance(:,2)./TotalDistance(:,3);
Distance;
TotalDistance;